<template>
  <el-dialog
    :visible="isshow"
    :title="{ add: '新增学科', edit: '编辑学科' }[mode]"
    @close="closeEvent"
    center
    width="600px"
  >
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item label-width="80px" label="学科编号" prop="rid">
        <el-input v-model="form.rid"></el-input>
      </el-form-item>
      <el-form-item label-width="80px" label="学科名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label-width="80px" label="学科简称" prop="short_name">
        <el-input v-model="form.short_name"></el-input>
      </el-form-item>
      <el-form-item label-width="80px" label="学科简介" prop="intro">
        <el-input v-model="form.intro"></el-input>
      </el-form-item>
      <el-form-item label-width="80px" label="学科备注" prop="remark">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div>
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { subjectAdd, subjectEdit } from '@/api/subject.js'
export default {
  // model:{
  //     prop:'value',
  //     event:'input',
  //     },
  //   mode: add,
  name: 'Add',
  props: ['isshow'],
  data () {
    return {
      mode: 'add',
      form: {
        rid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: '',
        status: ''
      },
      rules: {
        rid: [{ required: true, message: '请输入相应文字', trigger: 'change' }],
        name: [
          { required: true, message: '请输入相应文字', trigger: 'change' }
        ],
        short_name: [
          { required: false, message: '请输入相应文字', trigger: 'change' }
        ],
        intro: [
          { required: false, message: '请输入相应文字', trigger: 'change' }
        ],
        remark: [
          { required: false, message: '请输入相应文字', trigger: 'change' }
        ],
        status: [
          { required: false, message: '请输入相应文字', trigger: 'change' }
        ]
      }
      //   show:true
    }
  },
  methods: {
    created () {
      this.$emit('update:isshow', true)
    },
    closeEvent () {
      this.$emit('update:isshow', false)
      this.$refs.form.resetFields()
      this.form = {
        rid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: '',
        status: ''
      }
    },
    submit () {
      this.$refs.form.validate(async (result) => {
        if (result) {
          if (this.mode === 'add') {
            await subjectAdd(this.form)
            this.$message.success('新增成功')
          } else if (this.mode === 'edit') {
            await subjectEdit(this.form)
            this.$message.success('编辑成功')
            this.$emit('getData')
          }
          console.log('新增列表')
          this.$emit('getData')
          this.$emit('update:isshow', false)
        }
      })
    }
  }
  //  computed: {
  //     title () {
  //       return { add: '新增学科', edit: '编辑学科' }[this.mode]
  //     }
  //   }
}
</script>

<style></style>
